/**
* @author attilax 1466519819@qq.com
* @version  c0
* Copyright 2013 attilax reserved.
* All content (including but not limited to text, pictures, etc.) have copyright restrictions, the note license.
* all the software source code  copyright belongs to the original owner.
* Creative Commons protocol use please follow the "sign for non-commercial use consistent"; we do not welcome the large-scale duplication, and all rights reserved.
* Commercial sites or unauthorized media may not copy software source code.
**/
 
 


 
package com.mijie.homi.search.service.mood;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mijie.homi.common.domain.mood.Mood;
import com.mijie.homi.search.dao.mood.MoodDao;
//import com.mijie.homi.service.user.UserService;
import com.mijie.homi.search.service.index.dbc4Ibatis;
import com.mijie.homi.search.util.DbNdsController;
import com.mijie.homi.search.util.Dbcontroller;
import com.mijie.homi.search.util.god;

@Service
/**
* @author attilax 1466519819@qq.com
* @version  c0
**/
public class MoodService {

	@Autowired
	public MoodDao dao;
	
/**
* @author attilax 1466519819@qq.com
* @version  c0
**/
	public Mood getRecentlyMood(int userId){
		return dao.getRecentlyMood(userId);
	}
	
/**
* @author attilax 1466519819@qq.com
* @version  c0
**/
	public Mood getMoodById(long id){
		return dao.getMoodById(id);
	}
	
/**
* @author attilax 1466519819@qq.com
* @version  c0
**/
	public List<Mood> listRecentlyLoginUserMood(String atleastLastLoginAt,int offset,int limit){
		return dao.listRecentlyLoginUserMood(atleastLastLoginAt,offset, limit);
	}
	
/**
* @author attilax 1466519819@qq.com
* @version  c0
**/
	public boolean haveMoodViewPrivilege(int viewer,long id){
		return dao.haveMoodViewPrivilege(viewer, id);
	}
	@Autowired
	public dbc4Ibatis dbc4Ibatis;
	private Logger logger = Logger.getLogger(MoodService.class);
	/**
	 * 获取时间轴的最新心情的时候，不能获取被屏蔽的心情，如果最新一条是被屏蔽的心情，则跳到前一条
	 * @param userId
	 * @return
	 */
public Mood getRecentlyMoodNprblm(int userId) {
	
//	String sql="select SQL_NO_CACHE * from   (  select id,content,CREATEat,mood  from mood where userid=@uid  and markDelete=0 and block=0  union  select    id,title,CREATEat,moodtype mood   from topic where author=@uid  and markDelete=0  ) t    order by CREATEat desc limit 1";
//	sql=sql.replaceAll("@uid", String.valueOf( userId));
//	logger.info(sql);
//	Dbcontroller dbc=new DbNdsController();
//	ResultSet rs=dbc.getrs(sql);
	
	try {
		ResultSet rs = dbc4Ibatis
		.getrs("getMsgRecentlyOne", userId);
		Mood moodObj=new Mood();
		if(rs.next())
				{
		String moods=rs.getString("mood");
		if(moods.trim().length()==0)
			moods="7";
		String moodid=moods.split(",")[0];
		moodObj.setContent(rs.getString("content"));
		moodObj.setId( rs.getLong("id") );
		moodObj.setMood( Integer.parseInt(moodid) );
		
	//	moodObj.setContent("啊啊，家人反对我跟现在这个女朋友在一起");
	//	啊啊，家人反对我跟现在这个女朋友在一起
	//	dbc.close();
		return moodObj;}else
			return null;
	} catch (Exception e) {
	 
		logger.error(god.getTrace(e));
		return null;
	}
 
}
}